home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
src
/
haeberli
/
jpeg
/
jwrsgi.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
4KB
|
172 lines
/*
* jwrsgi.c
*
* Copyright (C) 1991, 1992, Thomas G. Lane.
* This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file.
*
* This file contains routines to write output images in SGI format.
*
* These routines may need modification for non-Unix environments or
* specialized applications. As they stand, they assume output to
* an ordinary stdio stream.
*
*
* These routines use the SGI image library functions found in -limage
*
* Paul Haeberli - 1993
*/
#include "jinclude.h"
#ifdef SGI_SUPPORTED
#include "gl/image.h"
#ifndef EIGHT_BIT_SAMPLES
Sorry, this code only copes with 8-bit JSAMPLEs. /* deliberate syntax err */
#endif
LOCAL short *rbuf;
LOCAL short *gbuf;
LOCAL short *bbuf;
LOCAL IMAGE *image;
LOCAL int cury;
LOCAL int outf;
/*
* Write the file header.
*/
LOCAL void
write_header (decompress_info_ptr cinfo, int num_colors)
/* Create and write a SGI header */
{
int xsize, ysize;
xsize = cinfo->image_width;
ysize = cinfo->image_height;
outf = dup(fileno(cinfo->output_file));
if (cinfo->out_color_space == CS_GRAYSCALE) {
image = (IMAGE *)fiopen(outf,"w",RLE(1),2,xsize,ysize,1);
rbuf = (short *)malloc(xsize*sizeof(short));
} else {
image = (IMAGE *)fiopen(outf,"w",RLE(1),3,xsize,ysize,3);
rbuf = (short *)malloc(xsize*sizeof(short));
gbuf = (short *)malloc(xsize*sizeof(short));
bbuf = (short *)malloc(xsize*sizeof(short));
}
if(!image)
ERREXIT(cinfo->emethods, "Could not write SGI header");
cury = 0;
}
/*
* Init output
*/
METHODDEF void
output_init (decompress_info_ptr cinfo)
{
if (cinfo->out_color_space == CS_GRAYSCALE) {
write_header(cinfo, 0);
} else if (cinfo->out_color_space == CS_RGB) {
write_header(cinfo, 0);
} else {
ERREXIT(cinfo->emethods, "SGI output must be grayscale or RGB");
}
}
/*
* Write some pixel data.
*/
METHODDEF void
put_RGB_pixel_rows (decompress_info_ptr cinfo, int num_rows,
JSAMPIMAGE pixel_data)
{
register JSAMPROW ptr0, ptr1, ptr2;
register long col;
long width = cinfo->image_width;
int row;
for (row = 0; row < num_rows; row++) {
ptr0 = pixel_data[0][row];
ptr1 = pixel_data[1][row];
ptr2 = pixel_data[2][row];
for (col = 0; col < width; col++) {
rbuf[col] = GETJSAMPLE(*ptr0);
gbuf[col] = GETJSAMPLE(*ptr1);
bbuf[col] = GETJSAMPLE(*ptr2);
ptr0++;
ptr1++;
ptr2++;
}
putrow(image,rbuf,image->ysize-1-cury,0);
putrow(image,gbuf,image->ysize-1-cury,1);
putrow(image,bbuf,image->ysize-1-cury,2);
cury++;
}
}
METHODDEF void
put_BW_pixel_rows (decompress_info_ptr cinfo, int num_rows,
JSAMPIMAGE pixel_data)
{
register JSAMPROW ptr0;
register long col;
long width = cinfo->image_width;
int row;
for (row = 0; row < num_rows; row++) {
ptr0 = pixel_data[0][row];
for (col = 0; col < width; col++) {
rbuf[col] = GETJSAMPLE(*ptr0);
ptr0++;
}
putrow(image,rbuf,image->ysize-1-cury,0);
cury++;
}
}
/*
* Finish up at the end of the file.
*/
METHODDEF void
output_term (decompress_info_ptr cinfo)
{
/* No work except to make sure we wrote the output file OK */
iclose(image);
fflush(cinfo->output_file);
if (ferror(cinfo->output_file))
ERREXIT(cinfo->emethods, "Output file write error");
free(rbuf);
if (cinfo->out_color_space == CS_RGB) {
free(gbuf);
free(bbuf);
}
}
/*
* The method selection routine for SGI format output.
* This should be called from d_ui_method_selection if SGI output is wanted.
*/
GLOBAL void
jselwsgi (decompress_info_ptr cinfo)
{
cinfo->methods->output_init = output_init;
if (cinfo->out_color_space == CS_GRAYSCALE)
cinfo->methods->put_pixel_rows = put_BW_pixel_rows;
else if (cinfo->out_color_space == CS_RGB)
cinfo->methods->put_pixel_rows = put_RGB_pixel_rows;
else
ERREXIT(cinfo->emethods, "SGI files can only be BW or RGB");
cinfo->methods->output_term = output_term;
}
#endif /* SGI_SUPPORTED */